<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模糊查询-列表过滤2</title>
    <script src="lib/vue.js"></script>
</head>
<style>
    li {
        list-style: none;
    }
</style>

<body>
    <div id="box">
        <input type="text" @input="handleInput()" v-model="mytext" />
        <ul>
            <li v-for="data in datalist" :key="data">{{data}}</li>
        </ul>


        <hr>
        <p>使用computed方法</p>
        <input type="text" v-model="mynumber" />
        <ul>
            <li v-for="data in handleComputed" :key="data">{{data}}</li>
        </ul>

    </div>

    <script>
        new Vue({
            el: "#box",
            data: {
                mytext: "",
                datalist: ['aaa', "bbb", "ccc", "ddd", "cee", "eee"],
                list: ['aaa', "bbb", "ccc", "ddd", "cee", "eee"],
                mynumber: ""

            },
            methods: {
                handleInput() {
                    //利用输入框的字符，过滤出包含字段的元素
                    console.log(this.mytext);
                    //filter使用回顾
                    // var arr = [1, 2, 3, 4, 5]
                    // var newlist = arr.filter(item => {
                    //     return item > 3
                    // })
                    //简写
                    //var newlist = arr.filter(item=>item>3)
                    // console.log(newlist);

                    var newlist = this.list.filter(item => item.indexOf(this.mytext) > -1)
                    console.log(newlist);
                    this.datalist = newlist
                }
            },
            computed: {
                handleComputed() {
                    return this.datalist.filter(item => item.indexOf(this.mynumber) > -1)
                }
            }
        })
    </script>

</body>

</html>